package com.frogs.ucenter.service.daoimpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.util.PageList;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.component.RequestApiHelper;
import com.frogs.ucenter.dao.SubFlowTaskRelationDao;
import com.frogs.ucenter.dao.TaskInfoDao;
import com.frogs.ucenter.entity.SubFlowTaskRelation;
import com.frogs.ucenter.entity.TaskInfo;
import com.frogs.ucenter.entity.TaskInfoExt;
import com.frogs.ucenter.service.TaskInfoService;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("taskInfoService")
public class TaskInfoServiceImpl extends BaseServiceImpl<TaskInfo> implements TaskInfoService {
    
	@Resource
    private TaskInfoDao taskInfoDao;
	
	@Resource
    private SubFlowTaskRelationDao subFlowTaskRelationDao;
    
	@Resource(name="requestApiHelper")
	private RequestApiHelper apiHelper;

    public TaskInfoServiceImpl() {
        super();
    }

    public TaskInfoDao getTaskInfoDao() {
        return taskInfoDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<TaskInfo> getBaseDao() {
        return taskInfoDao;
    }

    public void setTaskInfoDao(TaskInfoDao taskInfoDao) {
        this.taskInfoDao = taskInfoDao;
    }

	@Override
	public JSONArray getPromotionList(int sendCardType, int pageNo, int pageSize) {
		JSONObject json= apiHelper.getPromotionCouponList(sendCardType, pageNo, pageSize);
		JSONArray ja = json.getJSONObject("response").getJSONArray("list");
		return ja;
	}
	
	@Override
	public JSONArray getRedPackList(int pageNo, int pageSize) {
		JSONObject json= apiHelper.getRedPackList(pageNo, pageSize);
		JSONArray ja = json.getJSONObject("response").getJSONArray("list");
		return ja;
	}
	
	@Override
    public List<TaskInfoExt> getTaskInfoExtList(Map<String, Object> params) {
		return this.taskInfoDao.selectLeftJoinByMap(params);
	}

	@Override
	public void insertRelation(SubFlowTaskRelation subFlowTaskRelation) {
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("eqSubFlowId", subFlowTaskRelation.getSubFlowId());
		params.put("eqTaskId", subFlowTaskRelation.getTaskId());
		PageList<SubFlowTaskRelation> list = subFlowTaskRelationDao.selectByMap(params, 1, 1, false);
		if(list.size() == 0){
			subFlowTaskRelationDao.insert(subFlowTaskRelation);
		}else{
			throw new ServiceOperationException("该任务已经存在,不能再添加");
		}
	}

	@Override
	public int deleteRelation(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return subFlowTaskRelationDao.deleteByMap(param);
	}

	@Override
	public boolean isExistsTaskRelation(int subFlowId, int sequence) {
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("eqSubFlowId", subFlowId);
		params.put("eqSequence", sequence);
		if(subFlowTaskRelationDao.selectCountByMap(params)>0){
			return true;
		};
		return false;
	}
}